package pl.sind.http.basic;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;

import pl.sind.http.BaseHttpRequestResponse;
import pl.sind.http.BaseHttpResponse;
import pl.sind.http.HttpRequestException;
import pl.sind.http.HttpResponse;

public class UrlHttpResponse extends BaseHttpResponse<HttpURLConnection> {

	/**
	 * Constructor setting underlying connection.<br/>
	 * You'd better not give null here ;)
	 * 
	 * @param connection
	 *            to be used by wrapper.
	 */
	public UrlHttpResponse(HttpURLConnection connection) {
		super();
		wrap(connection);
	}

	public String getHeader(String contentType) {
		// TODO Auto-generated method stub
		return null;
	}

	public InputStream getInputStream() throws HttpRequestException {
		try {
			switch (getResponseCode()) {
			case 200:
			case 201:
				return connection.getInputStream();

			default:
				return connection.getErrorStream();
			}

		} catch (IOException e) {
			throw new HttpRequestException(e);
		}
	}

	public int getResponseCode() throws HttpRequestException {
		try {
			return connection.getResponseCode();
		} catch (IOException e) {
			throw new HttpRequestException(e);
		}
	}

	public String getResponseMessage() throws HttpRequestException {
		try {
			return connection.getResponseMessage();
		} catch (IOException e) {
			throw new HttpRequestException(e);
		}
	}

	public int getContentSize() {
		return connection.getContentLength();
	}

	public void close() {
		// TODO Auto-generated method stub

	}

}
